import React, { Component } from "react";
import { SafeAreaView, Text, View, StyleSheet, TouchableOpacity, Image, TextInput, Clipboard, Platform, DeviceEventEmitter, Switch } from 'react-native';
import Constant from '../../utils/Constant'//全局变量
import { getImgUrl } from '../../utils/ComFunction';
import HttpUtils from "../../utils/HttpUtils";
import HttpUrls from "../../utils/HttpUrls";
import Header from "../../components/header";
import { ToastToUser } from "../../utils/CustomToast";
import ActionSheet from 'react-native-actionsheet'
import ImageCropPicker from 'react-native-image-crop-picker';
import Upload from '../../components/upload/Upload'
import { requestCameraPermission, requestWritePermission } from '../../utils/Permission';
import Loading from "../../utils/Loading";
import AndroidModule from "../../utils/AndroidConnent";
import CommonDialog from "../../components/dialog/CommonDialog";
const isIos = Platform.OS == 'ios';
const isAndroid = Platform.OS == 'android';
import OpenIMSDKRN from "open-im-sdk-rn";

export default class Setting extends Component {

    constructor(props) {
        super(props);
        this.state = {
            release: Constant.release,
            isPushEnabled: true,
            dialogVisible: false,
            registrationID:''
        };
    }

    componentDidMount() {
        
    }

    render() {
        const navigation = this.props.navigation;
        return (
            <View
                style={styles.container}>
                <Header navigation={navigation} Title={'设置'} />


                {isAndroid ?
                    <View
                        style={styles.itemContain}>
                        <Text style={styles.itemTitle}>消息推送</Text>

                        <Switch
                            value={this.state.isPushEnabled}
                            onValueChange={(change) => {
                                this.setState({ isPushEnabled: change });
                            }}
                            trackColor={{ true: "#ffde00", false: "#eeeeee" }}
                            thumbColor={this.state.isPushEnabled ? "#f2d500" : "#f4f3f4"}
                        >

                        </Switch>
                    </View>
                    : null
                }


                <TouchableOpacity
                    style={styles.itemContain}
                    onPress={() => {
                        navigation.navigate('WebView', { source: 'https://m.shuaiche.com/static/protocol/yeguanService.html' })
                    }}>
                    <Text style={styles.itemTitle}>用户协议</Text>
                    <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                </TouchableOpacity>

                <TouchableOpacity
                    style={styles.itemContain}
                    onPress={() => {
                        navigation.navigate('WebView', { source: 'https://m.shuaiche.com/static/protocol/yeguanPrivacy.html', title: '隐私协议' })
                    }}>
                    <Text style={styles.itemTitle}>隐私协议</Text>
                    <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                </TouchableOpacity>

                {isAndroid ?
                    <TouchableOpacity
                        style={styles.itemContain}
                        onPress={() => {
                            navigation.navigate('WebView', { source: 'https://m.shuaiche.com//static/protocol/sdk.html', title: '第三方信息共享清单' })
                        }}>
                        <Text style={styles.itemTitle}>第三方信息共享清单</Text>
                        <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                    </TouchableOpacity>

                    : null
                }


                {isAndroid ?
                    <TouchableOpacity
                        style={styles.itemContain}
                        onPress={() => {
                            AndroidModule.goToPermission()
                        }}>
                        <Text style={styles.itemTitle}>权限管理</Text>
                        <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                    </TouchableOpacity>
                    : null
                }


                <TouchableOpacity
                    style={styles.itemContain}
                    onPress={() => { navigation.navigate('AboutShuaiche') }}>
                    <Text style={styles.itemTitle}>关于帅车</Text>
                    <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                </TouchableOpacity>

                <TouchableOpacity
                    style={styles.itemContainBottom}
                    onPress={() => { navigation.navigate('AccountLogOff') }}>
                    <Text style={styles.itemTitle}>账号注销</Text>
                    <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                </TouchableOpacity>

                

                {!this.state.release ?
                    <TouchableOpacity
                        style={[styles.itemContainBottom, { marginTop: 10 }]}
                        onPress={() => { navigation.navigate('SwitchAPI') }}>
                        <Text style={styles.itemTitle}>APP环境切换</Text>
                        <Image style={styles.arrowImg} source={require('source/image/comm/icon_tree_arrow.png')}></Image>
                    </TouchableOpacity>
                    : null
                }


                <TouchableOpacity
                    style={styles.btnLoginOut}
                    onPress={() => {
                        this.setState({
                            dialogVisible: true
                        })
                    }}>
                    <Text style={styles.loginOutText}>退出登录</Text>
                </TouchableOpacity>

                <CommonDialog
                    visible={this.state.dialogVisible}

                    title={'退出登录'}
                    content={'眼睁睁地看着你，却无能为力'}
                    confrimText={'狠心退出'}
                    cancelText={'愉快留下'}
                    onConfrim={() => {
                        this.setState({
                            dialogVisible: false
                        })
                        this.loginOut()
                    }}
                    onCancel={() => {
                        this.setState({
                            dialogVisible: false
                        })
                    }}
                />

                <Loading onRef={(ref) => (this.Loading = ref)} />

            </View>
        );
    }

    loginOut() {

        // 退出登录成功
        this.Loading.show('退出登录中...')
        let params = {
            sessionId: Constant.userInfo.sessionId
        }
        HttpUtils.getRequest(HttpUrls.loginOut, params)
            .then((result) => {
                this.Loading.hide()
                let code = result.code
                if (code == 0) {
                    OpenIMSDKRN.logout(Constant.userInfo.username)
                        .then(() => {
                            console.log('IM账号退出成功')
                            this.logOutNext()
                        })
                        .catch(({ errCode, errMsg }) => {
                            console.log('IM账号退出失败')
                            console.log('logout', errCode + '-' + errMsg)
                            // 调用失败
                            this.logOutNext()
                            // ToastToUser('退出登录失败')
                        });

                }
            })
            .catch((error) => {
                this.Loading.hide()
            });




    }

    logOutNext() {
        
        if (isIos) {
            OpenIMSDKRN.unInitSDK(Constant.userInfo.username).then(() => {
                console.log('unInitSDK成功')
            })
                .catch(({ errCode, errMsg }) => {
                    // unInitSDK失败
                    console.log('unInitSDK失败')
                });
        }

        AndroidModule.logOffSuccess()
    }

};

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: '#f9f9f9',
    },


    btnConfirm: {
        height: 50,
        marginLeft: 15,
        marginRight: 15,
        backgroundColor: '#ffde00',
        justifyContent: 'center',
        alignItems: 'center',
        marginTop: 15,
        borderRadius: 4
    },

    itemContain: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',

        height: 50,
        backgroundColor: '#ffffff',
        paddingLeft: 15,
        paddingRight: 15,
        borderBottomColor: '#e4e4e4',
        borderBottomWidth: 0.3
    },
    itemContainBottom: {
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',

        height: 50,
        backgroundColor: '#ffffff',
        paddingLeft: 15,
        paddingRight: 15

    },
    itemTitle: {
        fontSize: 15,
        color: '#333333'
    },
    arrow: {
        fontSize: 18,
        color: '#333333',
    },

    btnLoginOut: {
        backgroundColor: '#fb3d3c',
        justifyContent: 'center',
        alignItems: 'center',
        height: 46,
        marginTop: 50,
        borderRadius: 23,
        margin: 15
    },
    loginOutText: {
        color: '#ffffff',
        fontSize: 14
    }

});